package Automated2;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Set;

public class Automated {
    /**
     * 屏幕截图
     * @throws IOException
     */
    public void method_1 () throws IOException {
        //驱动管理程序的自动化
        WebDriverManager.edgedriver().setup();
        EdgeOptions options = new EdgeOptions();

        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");
        //打开浏览器
        WebDriver driver = new EdgeDriver(options);
        //输入百度地址
        driver.get("https://www.baidu.com");

        //截图
        getScreenShot(driver);

        //关闭浏览器
        driver.quit();
    }
    private void getScreenShot(WebDriver driver) throws IOException {
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");//年月日
        SimpleDateFormat sim2 = new SimpleDateFormat("HH-mm-ss-SS");//时分秒毫秒

        String dirTime = sim1.format(System.currentTimeMillis());
        String fileTime = sim2.format(System.currentTimeMillis());

        String fileName = "./src/test/images/" + dirTime + "/" + fileTime + ".png";

        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcFile,new File(fileName));
    }

    /**
     * 关闭窗口
     */
    public void method_2() throws InterruptedException {
        WebDriverManager.edgedriver().setup();
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new EdgeDriver(options);
        driver.get("https://www.baidu.com");
        String cur_window = driver.getWindowHandle();
        driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(3) > a > span.title-content-title")).click();
        Thread.sleep(3000);
        Set<String> allWindow = driver.getWindowHandles();
        for(String window : allWindow) {
            if(!window.equals(cur_window)) {
                driver.switchTo().window(window);//切换到新的窗口
            }
        }
        driver.close();//关闭当前窗口
        Thread.sleep(3000);
        driver.switchTo().window(cur_window);//切换到原窗口
        driver.close();//关闭当前窗口
//        driver.quit();//关闭所有窗口
    }

    /**
     * 等待
     * 强制等待
     * 隐式等待
     * 显示等待
     */
    public void method_3() throws InterruptedException {
        WebDriverManager.edgedriver().setup();
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new EdgeDriver(options);
        driver.get("https://www.baidu.com");
        driver.findElement(By.cssSelector("#kw")).sendKeys("王一博");
        driver.findElement(By.cssSelector("#su")).click();
        //NoSuchElementException ---代码太快，网页还没加载出来--采取等待的方式
        //1.强制等待
//        Thread.sleep(3000);
//        SimpleDateFormat sim = new SimpleDateFormat("HH:mm:ss:SS");
//        driver.findElement(By.cssSelector("#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span"));

        //2.隐式等待3秒
//        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
//        System.out.println(sim.format(System.currentTimeMillis()));
//        driver.findElement(By.cssSelector("#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span"));

        //3.显示等待
        WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(3));
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")));
        driver.quit();
    }

    public void method_4() {
        WebDriverManager.edgedriver().setup();
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new EdgeDriver(options);
        driver.get("https://www.baidu.com");
        driver.findElement(By.cssSelector("#kw")).sendKeys("王一博");
        driver.findElement(By.cssSelector("#su")).click();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss-SS");
        System.out.println(sim.format(System.currentTimeMillis()));
        //隐式等待5秒
        driver.manage().timeouts().implicitlyWait(Duration.ofMillis(5000));

        //显示等待10秒
        WebDriverWait wait = new  WebDriverWait(driver,Duration.ofSeconds(10));
        //找一个没有的元素
        try {
            wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#\\31  > divaqa > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")));
        }catch (TimeoutException e) {
            System.out.println("no find, timeout");
        }
        System.out.println(sim.format(System.currentTimeMillis()));

        //结论：重复多次，打印执行后的时间为10s、11s
        driver.quit();
    }

    /**
     * 浏览器导航navigate
     */
    public void method_5() throws InterruptedException {
        WebDriverManager.edgedriver().setup();
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new EdgeDriver(options);
        driver.navigate().to("https://www.baidu.com");
        driver.findElement(By.cssSelector("#kw")).sendKeys("王一博");
        driver.findElement(By.cssSelector("#su")).click();

        //等待页面加载
        Thread.sleep(3000);

        //后退
        driver.navigate().back();
        Thread.sleep(3000);
        //刷新
        driver.navigate().refresh();
        Thread.sleep(3000);
        //前进
        driver.navigate().forward();
        Thread.sleep(3000);

        driver.quit();
    }

    /**
     * 弹窗
     */
    public void method_6 () throws InterruptedException {
        WebDriverManager.edgedriver().setup();
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new EdgeDriver(options);
        //1.警告弹窗
//        driver.navigate().to("file:///F:/homework/computer/Java/%E8%B5%84%E6%96%99/selenium-html/alert.html#");
//        //点击链接
//        driver.findElement(By.xpath("//*[@id=\"tooltip\"]")).click();
//        Thread.sleep(2000);
//        //警告弹窗
//        Alert alert = driver.switchTo().alert();
//        Thread.sleep(2000);
//
//        alert.accept();//确认
//        alert.dismiss(); //取消 - 警告弹窗中只有一个确认按钮效果一样
        //2.确认弹框
//        driver.navigate().to("file:///F:/homework/computer/Java/%E8%B5%84%E6%96%99/selenium-html/confirm.html");
//        driver.findElement(By.cssSelector("body > input[type=button]")).click();
//        Alert alert = driver.switchTo().alert();
//        Thread.sleep(2000);
//        System.out.println("弹窗上的信息: " + alert.getText());
//        Thread.sleep(2000);
//
//        alert.dismiss();//取消
//        Thread.sleep(2000);
//
//        driver.navigate().refresh();//刷新
//        Thread.sleep(2000);
//
//        driver.findElement(By.cssSelector("body > input[type=button]")).click();
//        alert.accept();//确认
//        Thread.sleep(2000);

        driver.get("file:///F:/homework/computer/Java/%E8%B5%84%E6%96%99/selenium-html/Prompt.html");
        driver.findElement(By.xpath("/html/body/input")).click();
        Alert alert = driver.switchTo().alert();
        Thread.sleep(2000);
        alert.sendKeys("world");
        Thread.sleep(2000);
        alert.accept();
        Thread.sleep(2000);
        driver.quit();
    }

    /**
     * 文件上传
     */
    public void method_7() throws InterruptedException {
        WebDriverManager.edgedriver().setup();
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new EdgeDriver(options);
        driver.get("file:///F:/homework/computer/Java/%E8%B5%84%E6%96%99/selenium-html/upload.html");;
        Thread.sleep(2000);
        driver.findElement(By.xpath("/html/body/div/div/input")).sendKeys("F:\\homework\\computer\\Java\\资料\\Typora.md");
        Thread.sleep(2000);
        driver.quit();
    }

    public void method_8() throws InterruptedException {
        WebDriverManager.edgedriver().setup();
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");
//        options.addArguments("-headless");//一般工作中都会设置为无头模式，不设置默认为有头
        options.setPageLoadStrategy(PageLoadStrategy.NONE);//完全不阻塞webdriver
//        options.setPageLoadStrategy(PageLoadStrategy.EAGER);//DOM(文件对象模型)加载完毕，但还存在其他资源没有加载完成(注入图片...)
//        options.setPageLoadStrategy(PageLoadStrategy.NORMAL);//默认值，等所有的资源都加载完成
        WebDriver driver = new EdgeDriver(options);
        driver.get("https://www.bilibili.com");
//        Thread.sleep(2000);
//        driver.findElement(By.cssSelector("#i_cecream > div.bili-feed4 > main > div.feed2 > div > div.container.is-version8 > div:nth-child(6) > div > div > div.bili-video-card__info > div > h3 > a")).click();
        driver.quit();
    }
}
